// pages/teacher/question-bank/question-bank.js
Page({
  data: {
    selectMode: false,
    currentTab: 0,
    tabs: ['全部题目', '我的题目', '收藏题目'],
    questions: [
      {
        id: 1,
        type: 'single_choice',
        question: '微信小程序的配置文件是？',
        options: ['app.js', 'app.json', 'app.wxss', 'app.wxml'],
        correctAnswer: 'B',
        points: 10,
        category: '小程序基础',
        createTime: '2025-10-20',
        useCount: 15,
        isCollected: false
      },
      {
        id: 2,
        type: 'multiple_choice',
        question: '以下哪些是小程序的生命周期函数？',
        options: ['onLoad', 'onShow', 'onReady', 'onCreate'],
        correctAnswer: 'ABC',
        points: 15,
        category: '小程序基础',
        createTime: '2025-10-18',
        useCount: 23,
        isCollected: true
      },
      {
        id: 3,
        type: 'fill_blank',
        question: '小程序的数据绑定使用______语法。',
        correctAnswer: '{{}}',
        points: 10,
        category: '小程序基础',
        createTime: '2025-10-15',
        useCount: 30,
        isCollected: false
      }
    ],
    selectedQuestions: [],
    showFilter: false,
    filterOptions: {
      categories: ['全部', '小程序基础', '组件使用', 'API调用', '性能优化'],
      types: ['全部', '单选题', '多选题', '填空题'],
      selectedCategory: 0,
      selectedType: 0
    }
  },
  onLoad(options) {
    if (options.selectMode === 'true') {
      this.setData({ selectMode: true })
    }
  },
  switchTab(e) {
    this.setData({
      currentTab: e.currentTarget.dataset.index
    })
  },
  toggleFilter() {
    this.setData({
      showFilter: !this.data.showFilter
    })
  },
  selectCategory(e) {
    this.setData({
      'filterOptions.selectedCategory': e.detail.value
    })
  },
  selectType(e) {
    this.setData({
      'filterOptions.selectedType': e.detail.value
    })
  },
  applyFilter() {
    this.setData({
      showFilter: false
    })
    wx.showToast({
      title: '筛选成功',
      icon: 'success'
    })
  },
  toggleSelect(e) {
    const id = e.currentTarget.dataset.id
    const { selectedQuestions } = this.data
    const index = selectedQuestions.indexOf(id)
    
    if (index > -1) {
      selectedQuestions.splice(index, 1)
    } else {
      selectedQuestions.push(id)
    }
    
    this.setData({ selectedQuestions })
  },
  confirmSelect() {
    const { selectedQuestions } = this.data
    if (selectedQuestions.length === 0) {
      wx.showToast({
        title: '请至少选择一道题目',
        icon: 'none'
      })
      return
    }
    
    wx.showToast({
      title: `已选择${selectedQuestions.length}道题目`,
      icon: 'success'
    })
    
    setTimeout(() => {
      wx.navigateBack()
    }, 1500)
  },
  toggleCollect(e) {
    const id = e.currentTarget.dataset.id
    const questions = this.data.questions.map(q => {
      if (q.id === id) {
        return { ...q, isCollected: !q.isCollected }
      }
      return q
    })
    
    this.setData({ questions })
    
    const question = questions.find(q => q.id === id)
    wx.showToast({
      title: question.isCollected ? '收藏成功' : '取消收藏',
      icon: 'success'
    })
  },
  editQuestion(e) {
    const id = e.currentTarget.dataset.id
    wx.showToast({
      title: '编辑题目',
      icon: 'none'
    })
  },
  deleteQuestion(e) {
    const id = e.currentTarget.dataset.id
    wx.showModal({
      title: '确认删除',
      content: '删除后将无法恢复，确定删除吗？',
      success: (res) => {
        if (res.confirm) {
          const questions = this.data.questions.filter(q => q.id !== id)
          this.setData({ questions })
          wx.showToast({
            title: '删除成功',
            icon: 'success'
          })
        }
      }
    })
  },
  createQuestion() {
    wx.navigateTo({
      url: '/pages/teacher/homework-create/homework-create'
    })
  }
})

